package com.simple.service.fee;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import com.simple.po.FeeOtherFee;
import com.simple.po.FeeReport;
import com.xunj.core.CoreDao;
import com.xunj.core.ParaUnit;
import com.xunj.service.system.CodeService;
import com.xunj.service.system.IReportEngine;
import com.xunj.util.Common;
import com.xunj.util.Util;

public class OtherFeeReportService implements IReportEngine{
	
	private CoreDao dao;

	public OtherFeeReportService(CoreDao dao) {
		this.dao = dao;
	}

	
	/**
	 * 费用报销明细信息导出
	 * @return
	 * @throws Exception
	 */
	public Map exportRewardsPunishmentTol() throws Exception
	{
		Map beans = new HashMap();
		HttpServletRequest request = ServletActionContext.getRequest();
		FeeOtherFee feeOtherFee = new FeeOtherFee();
		Map map=request.getParameterMap();
		Iterator its = map.entrySet().iterator();
		Util u = new Util();
		while (its.hasNext()) {
			Entry e = (Entry) its.next();
			String name = (String) e.getKey();
			if(name.indexOf("feeOtherFee.")!=-1)
			{
				String[] values = (String[]) e.getValue();
				if(values[0] !=null && !values[0].equals(""))
					u.setPropertyValue(feeOtherFee, name.substring(12, name.length()), values[0]);
			}
		}
		beans.put("feeOtherFee", feeOtherFee);
		//查询所有员工信息
		String empHql = "select new Map(" +
				"employeeId as employeeId," +
				"employeeName as employeeName," +
				"onJobSituation as onJobSituation" +
				") from EmpEmployee where state='在用' order by areaId,employeeName";
		List employeeList = dao.findAll(empHql);
		
		
		String hql = "select new Map(otherFeeId as otherFeeId," +
				"f.employeeId as employeeId," +
				"f.otherFeeAmount as otherFeeAmount," +
				"f.otherFeeType as otherFeeType," +
				"relationDataId as relationDataId)" +
				"from FeeOtherFee f " +
				"where f.state='在用' and f.reportState='已上报' and f.otherFeeType ='奖惩'";
		

		if(feeOtherFee.getAreaId()!=null&&!feeOtherFee.getAreaId().equals(""))
			hql+=" and areaId='"+feeOtherFee.getAreaId()+"'";

		
		if(feeOtherFee.getEmployeeName()!=null&&!feeOtherFee.getEmployeeName().equals(""))
			hql+=" and employeeName like '%"+feeOtherFee.getEmployeeName()+"%'";
			
		String otherFeeClassesLvl = request.getParameter("otherFeeClassesLvl");
		
		if(feeOtherFee.getOtherFeeClasses()!=null&&!feeOtherFee.getOtherFeeClasses().equals(""))
		{
			hql+=" and otherFeeClasses like '"+feeOtherFee.getOtherFeeClasses()+"%'";
			ServletContext ctx = ServletActionContext.getServletContext();
			List codeList = (List) ctx.getAttribute("CodeDict");
			String codeName = CodeService.getCodeNameByCodeId(codeList,feeOtherFee.getOtherFeeClasses());
			beans.put("title", codeName);
		}
		else
		{
			if(!otherFeeClassesLvl.equals(""))
				hql+=" and otherFeeClasses like '"+otherFeeClassesLvl+"%'";
			beans.put("title", "惩罚");
		}
		//期初数据
		String condition_initialBalance = "";
		condition_initialBalance+=" and otherFeeDate<'"+feeOtherFee.getOtherFeeYear()+"-"+feeOtherFee.getOtherFeeMonth()+"-01 00:00:00"+"'";
		//期末数据
		String condition_endingBalance = "";
		condition_endingBalance+=" and otherFeeDate>='"+feeOtherFee.getOtherFeeYear()+"-"+feeOtherFee.getOtherFeeMonth()+"-01 00:00:00"+"'";

		int maxDay = 0;
		int day = 1;

        Calendar calendar = Calendar.getInstance();
        /**
         * 实例化日历各个字段,这里的day为实例化使用
         */
        calendar.set(Integer.parseInt(feeOtherFee.getOtherFeeYear()),Integer.parseInt(feeOtherFee.getOtherFeeMonth()) - 1,day);
        /**
         * Calendar.Date:表示一个月中的某天
         * calendar.getActualMaximum(int field):返回指定日历字段可能拥有的最大值
         */
        maxDay = calendar.getActualMaximum(Calendar.DATE);
        
        condition_endingBalance+=" and otherFeeDate<='"+feeOtherFee.getOtherFeeYear()+"-"+feeOtherFee.getOtherFeeMonth()+"-"+maxDay+" 23:59:59"+"'";
        
		List list_initialBalance = dao.findAll(hql+condition_initialBalance);
		List list_endingBalance = dao.findAll(hql+condition_endingBalance);
		
		//组织查询对应资金上缴数据
		List relationDataIdList = new ArrayList();
		for(int i=0;i<list_initialBalance.size();i++){
			Map iMap = (Map) list_initialBalance.get(i);
			if(!relationDataIdList.contains(iMap.get("otherFeeId")))
			{
				relationDataIdList.add(iMap.get("otherFeeId"));
			}
		}
		for(int i=0;i<list_endingBalance.size();i++){
			Map eMap = (Map) list_endingBalance.get(i);
			if(!relationDataIdList.contains(eMap.get("otherFeeId")))
			{
				relationDataIdList.add(eMap.get("otherFeeId"));
			}
		}
		//查询资金上缴数据，放入奖惩中
		List moneyPayList = new ArrayList();
		if(!relationDataIdList.isEmpty())
		{
			String moneyPayHql = "select new Map(otherFeeId as otherFeeId," +
				"f.employeeId as employeeId," +
				"f.otherFeeAmount as otherFeeAmount," +
				"f.otherFeeType as otherFeeType," +
				"relationDataId as relationDataId)" +
				"from FeeOtherFee f " +
				"where f.state='在用' and relationDataId in (";
			for(int i=0;i<relationDataIdList.size();i++)
			{
				moneyPayHql+="'"+relationDataIdList.get(i)+"',";
			}
			moneyPayHql = moneyPayHql.substring(0,moneyPayHql.length()-1);
			moneyPayHql+=")";
			moneyPayList = dao.findAll(moneyPayHql);
		}
		
		List list = new ArrayList();
		//计算期初余额
		for(int i=0;i<list_initialBalance.size();i++){
			Map iMap = (Map) list_initialBalance.get(i);
			Map dataMap = null;
			//找到已保存的数据
			for(int j=0;j<list.size();j++)
			{
				Map tmpMap = (Map) list.get(j);
				if(iMap.get("employeeId").equals(tmpMap.get("employeeId")))
				{
					dataMap = tmpMap;
					break;
				}
			}
			//组织数据放入结果list
			if(dataMap==null)
			{
				dataMap = new HashMap();
				dataMap.put("employeeId", iMap.get("employeeId"));
				for(int j=0;j<employeeList.size();j++){
					Map employeeMap = (Map) employeeList.get(j);
					if(employeeMap.get("employeeId").equals(iMap.get("employeeId")))
					{
						dataMap.put("employeeName", employeeMap.get("employeeName"));
						dataMap.put("onJobSituation", employeeMap.get("onJobSituation"));
						break;
					}
				}
				//期初余额
				dataMap.put("initialBalance", 0d);
				//借方
				dataMap.put("debit", 0d);
				//贷方
				dataMap.put("credit", 0d);
				//期末余额
				dataMap.put("endingBalance", 0d);
				list.add(dataMap);
			}
			dataMap.put("initialBalance", Common.sum((Double)dataMap.get("initialBalance"),(Double)iMap.get("otherFeeAmount")));
			for(int j=0;j<moneyPayList.size();j++)
			{
				Map moneyPayMap = (Map) moneyPayList.get(j);
				//找到当前惩罚信息对应的资金上缴数据
				if(moneyPayMap.get("relationDataId").equals(iMap.get("otherFeeId")))
					dataMap.put("initialBalance", Common.sum((Double)dataMap.get("initialBalance"),(Double)moneyPayMap.get("otherFeeAmount")*-1));
			}
			
		}
		//计算当期发生的借贷数据
		for(int i=0;i<list_endingBalance.size();i++){
			Map eMap = (Map) list_endingBalance.get(i);
			Map dataMap = null;
			//找到已保存的数据
			for(int j=0;j<list.size();j++)
			{
				Map tmpMap = (Map) list.get(j);
				if(eMap.get("employeeId").equals(tmpMap.get("employeeId")))
				{
					dataMap = tmpMap;
					break;
				}
			}
			//组织数据放入结果list
			if(dataMap==null)
			{
				dataMap = new HashMap();
				dataMap.put("employeeId", eMap.get("employeeId"));
				for(int j=0;j<employeeList.size();j++){
					Map employeeMap = (Map) employeeList.get(j);
					if(employeeMap.get("employeeId").equals(eMap.get("employeeId")))
					{
						dataMap.put("employeeName", employeeMap.get("employeeName"));
						dataMap.put("onJobSituation", employeeMap.get("onJobSituation"));
					}
				}
				//期初余额
				dataMap.put("initialBalance", 0d);
				//借方
				dataMap.put("debit", 0d);
				//贷方
				dataMap.put("credit", 0d);
				//期末余额
				dataMap.put("endingBalance", 0d);
				list.add(dataMap);
			}
			dataMap.put("debit", Common.sum((Double)dataMap.get("debit"),(Double)eMap.get("otherFeeAmount")));
			for(int j=0;j<moneyPayList.size();j++)
			{
				Map moneyPayMap = (Map) moneyPayList.get(j);
				//找到当前惩罚信息对应的资金上缴数据
				if(moneyPayMap.get("relationDataId").equals(eMap.get("otherFeeId")))
					dataMap.put("credit", Common.sum((Double)dataMap.get("credit"),(Double)moneyPayMap.get("otherFeeAmount")));
			}
			//期末数据=期初+借方-贷方
			dataMap.put("endingBalance", Common.sum((Double)dataMap.get("initialBalance"),
					(Double)dataMap.get("debit"),
					(Double)dataMap.get("credit")*-1));
			
		}
		
		if(list.size()==0)
			request.setAttribute("errorMsg", "无符合所选条件数据！");
		
		beans.put("datalist", list);

		return beans;
	}

		
		
}
